package com.hzhc.controller.charge;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.entity.ChargeItem;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.ChargeContractService;
import com.hzhc.service.ChargeItemService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.ChargeContractVo;
import com.hzhc.vo.ChargeItemVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author dongcy
 * @date 2024/6/26 15:05
 * @description ChargeItemController
 */
@RestController
@RequestMapping("/chargeItem")
@RequiredArgsConstructor
public class ChargeItemController extends BaseController {

    private final ChargeItemService itemService;

    private final ChargeContractService contractService;

    /**
     * 计费项目 list
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/getAllList")
    @ResponseBody
    public ResultUtil<List<ChargeItemVo>> getAllList(HttpServletRequest request, @RequestBody ChargeItemVo vo){
        Page<ChargeItemVo> page = new Page<>(vo.getPageIndex(),vo.getPageSize());
        ChargeContractVo chargeContractVo = new ChargeContractVo();
        chargeContractVo.setCompanyId(vo.getCompanyId());
        chargeContractVo.setContractName(vo.getCompanyName());
        List<ChargeContractVo> chargeContractList = contractService.getYzsChargeContractList(new Page<>(vo.getPageIndex(),vo.getPageSize()),chargeContractVo);
        if(chargeContractList.isEmpty()){
            return ResultUtil.build(new ArrayList<>(),ResultUtilCodeEnum.SUCCESS);
        }
        List<ChargeItemVo> list = itemService.getAllList(page,vo);
        return ResultUtil.build(list,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 计费项目 add
     * @param request
     * @param item
     * @return
     */
    @PostMapping("/addChargeItem")
    @SysTemLog(operModul = "计费管理-计费项目",operType = "ADD",operDesc = "计费项目添加",modular="PC")
    public ResultUtil<T> addChargeItem(HttpServletRequest request, @RequestBody ChargeItem item){
        Long userID = TokenUtil.getUserID(request);
        item.setUserId(userID);
        item.setCreateTime(LocalDateTime.now());
        return itemService.addChargeItem(item);
    }

    /**
     * 计费项目 update
     * @param request
     * @param item
     * @return
     */
    @PostMapping("/updateChargeItem")
    @SysTemLog(operModul = "计费管理-计费项目",operType = "UPDATE",operDesc = "计费项目修改",modular="PC")
    public ResultUtil<T> updateChargeItem(HttpServletRequest request,@RequestBody ChargeItem item){
        Long userID = TokenUtil.getUserID(request);
        item.setUpdateUserId(userID);
        item.setUpdateTime(LocalDateTime.now());
        return itemService.updateChargeItem(item);
    }

    /**
     * 计费项目 delete
     * @param id
     * @return
     */
    @GetMapping("/delChargeItem/{id}")
    @SysTemLog(operModul = "计费管理-计费项目",operType = "DELETE",operDesc = "计费项目删除",modular="PC")
    public ResultUtil<T> delChargeItem(@PathVariable("id") Long id){
        return itemService.delChargeItem(id);
    }

    /**
     * 计费配置数据接口
     * @param vo
     * @return
     */
    @RequestMapping("/getChargeItemList")
    public ResultUtil<List<ChargeItemVo>> getChargeItemList(@RequestBody ChargeItemVo vo){
        List<ChargeItemVo> newchoose = itemService.selectChargeItemList(vo);
        return ResultUtil.ok(newchoose);
    }

}
